/**
 * @author David Young
 */
package csab.game.sprites;


import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.PackedSpriteSheet;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.util.Log;

import csab.core.Sprite;
import csab.game.PlayerController;

/**
 * This Sprite is meant to represent the
 * player in game.
 * @author David Young
 */
public class PlayerSprite extends Sprite {
	private SpriteSheet walkingleft;
	private SpriteSheet walkingright;
	private SpriteSheet idleleft;
	private SpriteSheet idleright;
	private SpriteSheet slashLeft;
	private SpriteSheet slashRight;
	private SpriteSheet dying;
	private boolean doprepareattk;
	/**
	 * The constructor for this class.
	 * @param name The name to assign to this
	 * Entity.
	 * @throws SlickException 
	 * @see csab.core.Entity
	 */
	public PlayerSprite(String name) throws SlickException {
		super(name);
		doprepareattk = false;
		PackedSpriteSheet sheet = new PackedSpriteSheet("resource/ss/Warrior.def",
														new Color(0,0,0));
		
		walkingleft = sheet.getSpriteSheet("warriorwalkingleft");
		walkingright = sheet.getSpriteSheet("warriorwalkingright");
		idleleft = sheet.getSpriteSheet("warrioridleleft");
		idleright = sheet.getSpriteSheet("warrioridleright");
		dying = sheet.getSpriteSheet("warriordying");
		slashLeft = sheet.getSpriteSheet("warriorslashleft");
		slashRight = sheet.getSpriteSheet("warriorslashright");
		
		Animation death = new Animation(dying, 0, 0, 
				dying.getHorizontalCount() - 1, 
				dying.getVerticalCount() - 1, true, 200, true);
		death.setPingPong(false);
		
		Animation sl = new Animation(slashLeft, 0, 0, 
				slashLeft.getHorizontalCount() - 1, 
				slashLeft.getVerticalCount() - 1, true, 200, true);
		sl.setPingPong(false);
		
		Animation sr = new Animation(slashRight, 0, 0, 
				slashRight.getHorizontalCount() - 1, 
				slashRight.getVerticalCount() - 1, true, 200, true);
		sr.setPingPong(false);
		
		Animation animlw = new Animation(walkingleft, 0, 0, 
				walkingleft.getHorizontalCount() - 1, 
				walkingleft.getVerticalCount() - 1, true, 200, true);
		animlw.setPingPong(true);
		
		Animation animrw = new Animation(walkingright, 0, 0, 
				walkingright.getHorizontalCount() - 1, 
				walkingright.getVerticalCount() - 1, true, 200, true);
		animlw.setPingPong(true);
		
		Animation animlidle = new Animation(idleleft, 0, 0, 
				idleleft.getHorizontalCount() - 1, 
				idleleft.getVerticalCount() - 1, true, 200, true);
		animlw.setPingPong(true);
		
		Animation animridle = new Animation(idleright, 0, 0, 
				idleright.getHorizontalCount() - 1, 
				idleright.getVerticalCount() - 1, true, 200, true);
		animlw.setPingPong(true);
		
		Image[] fwd = new Image[1];
		fwd[0] = animridle.getImage(0);
		Animation animfwd = new Animation(fwd,1);
		animfwd.setLooping(false);
		animfwd.setAutoUpdate(false);
		
		this.setWidth(fwd[0].getWidth());
		this.setHeight(fwd[0].getHeight());
		
		this.setController(new PlayerController(this));
		super.initAnims(animlw, animrw, animlidle, animridle, animfwd, death, sl, sr);
	}
	
	/* (non-Javadoc)
	 * @see csab.core.Entity#destroy()
	 */
	@Override
	protected void destroy() {
		try {
			this.walkingleft.destroy();
			this.walkingright.destroy();
			this.idleleft.destroy();
			this.idleright.destroy();
			this.slashLeft.destroy();
			this.slashRight.destroy();
		} catch (SlickException e) {
			Log.debug("Error freeing animations! " + this.getName());
		}
	}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#canAttack()
	 */
	@Override
	public boolean canAttack() {
		return true;
	}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#doAttack()
	 */
	@Override
	public void doAttack() {
	}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#prepareAttack()
	 */
	@Override
	public void prepareAttack() {
		doprepareattk = true;
	}
	
	/* (non-Javadoc)
	 * @see csab.core.Sprite#doAction(int)
	 */
	@Override
	public void doAction(int delta) {
		super.doAction(delta);
		if(doprepareattk)
		{
			this.animAttackRight.setAutoUpdate(true);
			this.animAttackRight.setLooping(false);
			this.animAttackRight.restart();
			this.animAttackLeft.setAutoUpdate(true);
			this.animAttackLeft.setLooping(false);
			this.animAttackLeft.restart();
			doprepareattk = false;
		}
	}
}
